package com.abbas.abchat.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;

import com.abbas.abchat.R;
import com.abbas.abchat.entity.Replay;

import java.util.List;

/**
 * @author abbas
 * @description: TODO
 * @date 2022/4/21 13:32
 */
public class ReplayAdapter extends BaseAdapter {

    private Context context;
    private List<Replay> list;

    public ReplayAdapter(Context context, List<Replay> list) {
        this.context = context;
        this.list = list;
    }

    public void setList(List<Replay> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context)
                    .inflate(R.layout.replay_item, parent, false);
            viewHolder.key = (EditText) convertView.findViewById(R.id.key);
            viewHolder.value = (EditText) convertView.findViewById(R.id.value);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        Replay replay = list.get(position);
        viewHolder.key.setText(replay.getKey());
        viewHolder.value.setText(replay.getValue());

        viewHolder.key.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                list.get(position).setKey(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        viewHolder.value.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                list.get(position).setValue(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

//        EditText key = viewHolder.key;
//        EditText value = viewHolder.value;

//        viewHolder.key.setOnFocusChangeListener(new View.OnFocusChangeListener() {
//            @Override
//            public void onFocusChange(View v, boolean hasFocus) {
//                list.get(position).setKey(key.getText().toString().trim());
//            }
//        });
//
//        viewHolder.value.setOnFocusChangeListener(new View.OnFocusChangeListener() {
//            @Override
//            public void onFocusChange(View v, boolean hasFocus) {
//                list.get(position).setValue(value.getText().toString().trim());
//            }
//        });

        return convertView;
    }

    static class ViewHolder {
        EditText key;
        EditText value;
    }
}
